package com.today.usercenter.ui.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.today.usercenter.R;
import com.today.usercenter.network.entity.TaskInfoEntity;

import java.util.List;

/**
 * Created by 88426 on 2017/9/6.
 */

public class TaskListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<TaskInfoEntity.DataBean> mTasks;
    private Context mContext;

    public TaskListAdapter(List<TaskInfoEntity.DataBean> data, Context context) {
        mTasks = data;
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.task_list_item, parent, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TaskInfoEntity.DataBean item = mTasks.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.title.setText(item.title);
        viewHolder.gain.setText("+" + item.score);
        viewHolder.rule.setText(String.format(mContext.getString(R.string.task_rule_format), item.score, item.count, item.threshold));
    }

    @Override
    public int getItemCount() {
        return mTasks.size();
    }


    static public class ViewHolder extends RecyclerView.ViewHolder {
        public View root;
        public TextView title;
        public TextView gain;
        public TextView rule;

        public ViewHolder(View root) {
            super(root);

            this.root = root;
            this.title = root.findViewById(R.id.task_title);
            this.gain = root.findViewById(R.id.task_gain);
            this.rule = root.findViewById(R.id.task_rule);
        }
    }
}
